<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	String type = (String)request.getAttribute("type");
	String userId = (String)request.getAttribute("userId");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>新增会员</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<jsp:include page="/page/head.jsp"></jsp:include>
	<script type="text/javascript">
		var type = "<%=type%>";	
		var userId = "<%=userId%>";	
	
	    //打开页面判断操作类型是否为edit，为edit则在加载事件根据url参数中传递过来的userId查找出当前用户的信息作出修改
	
		//保存信息
		function save(){
			
			$('#saveBtn').attr('disabled','disabled');
			
			var cardid = escape(escape($('#cardid').val()));         		//获取cardid值
			var mobile = escape(escape($('#mobile').val()));         		//获取手机号码
			var sex = escape(escape($('#sex').val()));                   	//获取sex值
			var password = escape(escape($('#password').val()));         	//获取密码值
			var confirmPassword = escape(escape($('#confirmPassword').val()));    //获取confirmPassword值
			var truename = escape(escape($('#truename').val()));            //获取truename 
			var username = escape(escape($('#username').val()));         	//获取username值
			var userType = 1;         //添加会员，userType默认为1
			var email = escape(escape($('#email').val()));               	//获取truename值
			
			var orgId = $('#orgId').val();
			var orgName = escape(escape($('#orgName').val()));
			var orgType = $('#orgType').val();
			
			if($('#fm').form('validate') && flag1 && flag2){
				//做操作类型判断
				var url = "";
					if(type == "add"){
						//add处理路径
						url = path +"/member/save.do?cardid="+cardid+"&sex="+sex+"&username="+username+"&email="+email+"&mobile="+mobile+"&truename="+truename+"&orgId="+orgId+"&orgName="+orgName+"&orgType="+orgType;
					}else{
						//update处理
						url = path +"/member/update.do?userId="+userId+"&truename="+truename+"&sex="+sex+"&email="+email;
					}
				
					//执行ajax操作
					$.ajax({
						  url:url,
						  success: function(data) {
							  if(data == "1"){
								  window.location = path+"/member/memberMgr.do";
							   }else{
								   alertBox("操作失败！");
							   } 
						  }
					});
			}
		}
		
		//撤销回退
		function callback(){
			window.location = path+"/member/memberMgr.do";
		}
		
	</script>
	
	<script type="text/javascript">
		//邮箱验证
		$('#email').validatebox({  
		    required:true  
		});
		
		//自定义规则
		$.extend($.fn.validatebox.defaults.rules, {  
			chs: {    									  //验证汉子
		        validator: function (value) {
		            return /^[\u0391-\uFFE5]+$/.test(value);
		        },
		        message: '只能输入汉字'
		    },
		    mobile:{                                      //手机号码长度必须只能为11个，不能再某些数字之间
		    	validator: function (value) {
		    		//^[1][3-8]+\\d{9}
		            return /^1[3|4|5|8][0-9]\d{4,8}$/.test(value) ;
		        },
		        message: '输入正确的手机号码!'
		    },
		    checkUserNameExists:{
		    	validator: function (value) {
					return /^[\w]+$/.test(value);
		    	},
		    	message:'只能数字、字母、下划线组成!'
		    }
		});
		
		var flag1 = true;
		
		function checkUserName(){
			$.ajax({
			  url:path + "/user/checkUserNameExists.do?username="+$('#username').val(),
			  success: function(data) {
				   if(data == "1"){
					   flag1 = false;
					   alertBox("会员名无效！");
				   }
			  }
			});
		}
		
		var flag2 = true;
		function checkMobile(){
			$.ajax({
			  url:path + "/user/checkMobileExists.do?mobile="+$('#mobile').val(),
			  success: function(data) {
				   if(data == "1"){
					   flag2 = false;
					   alertBox("手机号码无效！");
				   }
			  }
			});
		}
		
		function selectOrg(){
			var w =(screen.width-650)/2;
			var h = (screen.height-500)/2;
			var ret = window.showModalDialog(path+"/page/common/select-org.jsp",window,"dialogWidth:650px;dialogHeight:500px;dialogTop:"+h+"px;dialogLeft:"+w+"px;help:no;status:no;center:yes");
			if(ret != null){
				$('#orgId').attr('value',ret[0]);
				$('#orgName1').attr('value',ret[1]);
				$('#orgName').attr('value',ret[1]);
				$('#orgType').attr('value',ret[2]);
				
			}
		}
	</script>
  </head>
  
  <body>
   <div class="right">
	<h1><b>${type=='add'?'新增会员':'编辑会员' }</b><span><img src="<%=basePath%>images/right_h1bg2.jpg" width="3" height="29" /></span></h1>
	<div class="ri_main">
	<form id="form">
		<table width="100%" border="0" cellspacing="1" cellpadding="8" style="background:#d9d9d9;">
		   <c:choose>
		   	<c:when test="${onlineUser.userType == 6 }">
		   		 <tr>
					<td width="15%" align="center" bgcolor="#FFFFFF">所属商家</td>
				    <td width="40%" bgcolor="#FFFFFF" align="left" colspan="3"><label for="textfield"></label>
				    	<input type="hidden" id="orgId" name="orgId" />
				    	<input type="hidden" id="orgName" name="orgName" />
				    	<input type="hidden" id="orgType" name="orgType" />
				    	<c:if test="${type=='add'}">
		   					<input class="easyui-validatebox" disabled="disabled" required="true" style="width:200px;" id="orgName1" name="orgName1" />
		   					<input type="button" value="选择" onclick="selectOrg()">
		   				</c:if>
		   				<c:if test="${type=='edit'}">
					      <lable>${user != null?user.orgName:'' }</lable>
					    </c:if>
				    </td>
				  </tr>
		   	</c:when>
		   	<c:otherwise>
		   		<input type="hidden" id="orgId" name="orgId" value="${onlineUser.orgId }" />
		   		<input type="hidden" id="orgName" name="orgName" value="${onlineUser.orgName }" />
		   		<input type="hidden" id="orgType" name="orgType" value="${onlineUser.orgType }" />
		   	</c:otherwise>
		   </c:choose>
		  <tr>
			<td width="15%" align="center" bgcolor="#FFFFFF">会员卡号</td>
		    <td width="40%" bgcolor="#FFFFFF" align="left" colspan="3"><label for="textfield"></label>
		    <c:if test="${type=='add'}">
		      <input id="cardid" name="cardid" class="easyui-validatebox" required="true" style="width:200px;" value="${user != null?user.card.id:'' }" />
		      * 请设置卡号
		    </c:if>
		    <c:if test="${type=='edit'}">
		      <lable>${user != null?user.card.id:'' }</lable>
		    </c:if>
		    </td>
		  </tr>
		   <tr>
			<td width="15%" align="center" bgcolor="#FFFFFF">真实姓名</td>
		    <td width="40%" bgcolor="#FFFFFF" align="left" colspan="3"><label for="textfield"></label>
		      <input validType="chs" id="truename" name="truename" class="easyui-validatebox" required="true" style="width:200px;" value="${user != null?user.truename:'' }" />
		      * (汉字)
		    </td>
		  </tr>
		  <tr>
		    <td width="15%" align="center" bgcolor="#FFFFFF">登录名</td>
		    <td width="40%" bgcolor="#FFFFFF" align="left"><label for="textfield"></label>
		      <input  onblur="checkUserName()" id="username" name="username" class="easyui-validatebox" required="true" style="width:200px;" value="${user != null?user.truename:'' }" />
		    </td>
		    <td width="15%" align="center" bgcolor="#FFFFFF">性&nbsp;&nbsp;&nbsp;别</td>
		    <td width="30%" align="left" bgcolor="#FFFFFF">
		    	<select id="sex" name="sex">
		    		<c:forEach var="dict" items="${sexTypes}">
		    			<c:choose>
		    				<c:when test="${user != null && dict.code == user.sex}">
		    					<option value="${dict.code }" selected="selected"> ${dict.name } </option>
		    				</c:when>
		    				<c:otherwise>
		    					<option value="${dict.code }"> ${dict.name } </option>
		    				</c:otherwise>
		    			</c:choose>
		    		</c:forEach>
		    	</select>
		    </td>
		  </tr>
		   <c:if test="${type=='add'}" >
			  <tr>
			    <td align="center" bgcolor="#FFFFFF">手机号码</td>
			    <td bgcolor="#FFFFFF" colspan="3" align="left">
			    	<input validType="mobile" onblur="checkMobile()" id="mobile" name="mobile" class="easyui-validatebox" required="true" style="width:200px;" value="${user != null?user.mobile:'' }" />
			    	*(发送用户信息到该手机)
			    </td>
			  </tr>
		  </c:if>
		  <tr>
		    <td align="center" bgcolor="#FFFFFF">邮&nbsp;&nbsp;&nbsp;箱</td>
		    <td bgcolor="#FFFFFF" colspan="3" align="left">
		    	<input validType="email" id="email" name="email" class="easyui-validatebox" required="true" style="width:200px;" value="${user != null?user.email:'' }" />
		    </td>
		  
		  <tr>
		    <td bgcolor="#FFFFFF" colspan="4" align="center">
		    	<a href="javascript:void(0);" id="saveBtn" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">保存</a>
				<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" onclick="callback()">取消</a>
		    </td>
		  </tr>
		</table>
	</form>
	</div>
	</div>
  </body>
</html>
